<template>
	<view :data-theme="themeStyle">
		<view>
			<view class="combo-package" :class="isIphoneX ? 'combo-iphonex' : ''">
				<view class="combo-package-list">
					<view class="combo-package-content">
						<view class="combo-package-name ns-text-color-black ns-bg-color-gray-fadeout-60">
							<view class="iconfont iconyuan_checked ns-text-color"></view>
							<text class="ns-margin-right">套餐名称：{{combo.bl_name}}</text>
						</view>

						<view v-for="(item, index) in combo.bundling_goods" :key="index" class="goods-info" @click="toGoodsDetail(item.sku_id)">
							<view class="goods-img">
								<view class="img-wrap">
									<image :src="$util.img(item.sku_image, { size: 'mid' })" @error="imageError(index)" mode="aspectFit" />
								</view>
							</view>
							<view class="data-info">
								<view class="goods-name">{{ item.sku_name }}</view>
								<view class="price-wrap">
									<text class="uni-bold ns-text-color">{{ $lang('common.currencySymbol') }} {{ item.price }}</text>
									<text class="num">x1</text>
								</view>
								<view class="stock-tips ns-text-color" v-show="item.stock < num">
									库存不足，剩余：{{ item.stock }}
									<block v-if="item.unit">{{ item.unit }}</block>
									<block v-else>件</block>
								</view>
							</view>
						</view>
					</view>
				</view>

				<view class="footer" :class="isIphoneX ? 'padding-bottom' : ''">
					<view class="footer-info">
						<text class="info-price">
							<text class="ns-font-size-sm">套餐价：</text>
							<text class="ns-font-size-x-sm ns-text-color">{{ $lang('common.currencySymbol') }}</text>
							<text class="ns-text-color ns-font-size-x-lg">{{ packagePrice[0] }}.</text>
							<text class="ns-text-color ns-font-size-x-sm">{{ packagePrice[1] }}</text>
						</text>
						<text class="info-num">为您节省：{{ $lang('common.currencySymbol') }} {{ saveThePrice }} </text>
					</view>
					<view class="footer-btn ns-bg-color" @click="comboBuy()">立即购买({{ num }})</view>
				</view>
			</view>

			<loading-cover ref="loadingCover"></loading-cover>
			<ns-login ref="login"></ns-login>
		</view>
		<ns-show-toast></ns-show-toast>
	</view>
</template>

<script>
	import uniNumberBox from '@/components/uni-number-box/uni-number-box.vue';
	import globalConfig from '@/common/js/golbalConfig.js'
	import nsShowToast from '@/components/ns-show-toast/ns-show-toast.vue'
	export default {
		components: {
			uniNumberBox,
			nsShowToast
		},
		data() {
			return {
				num: 1, //购买数量
				blId: 0, //商品id
				combo: [],
				packagePrice: [], //套餐价
				saveThePrice: 0, //节省价格
				isDisabled: false, //按钮失效
				isIphoneX: false //判断是否是iphoneX以上的设备
			};
		},
		mixins:[globalConfig],
		onLoad(e) {
			this.blId = e.bl_id || 0;
			this.isIphoneX = this.$util.uniappIsIPhoneX();
		},
		onShow() {
			// 刷新多语言
			this.$langConfig.refresh();
			this.getDetail();
		},
		onHide() {
			this.btnSwitch = true;
		},
		methods: {
			// 获取套餐列表
			getDetail() {
				this.$api.sendRequest({
					url: '/bundling/api/bundling/detail',
					data: {
						bl_id: this.blId
					},
					success: res => {
						if (res.data) {
							this.combo = res.data;
							this.numberChange();
						} else {
							this.$util.showToast({
								title: res.message
							});
						}
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					},
					fail: res => {
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			},
			toGoodsDetail(id){
				this.$util.redirectTo('/pages/goods/detail/detail',{sku_id:id})
			},
			numberChange(flag, callback) {
				setTimeout(() => {
					var disabledCount = 0;

					// 防止空
					if (flag && this.num.length == 0) {
						this.num = 1;
						disabledCount++;
					}

					// 防止输入0和负数、非法输入
					if (flag && (this.num <= 0 || isNaN(this.num))) {
						this.number = 1;
						disabledCount++;
					}

					if (flag) this.num = parseInt(this.num);

					var price = 0;
					for (var i = 0; i < this.combo.bundling_goods.length; i++) {
						price += parseFloat(this.combo.bundling_goods[i].price);
						//检测库存
						if (this.combo.bundling_goods[i].stock < this.num) disabledCount++;
					}
					this.isDisabled = disabledCount > 0;
					this.saveThePrice = ((price - this.combo.bl_price) * this.num).toFixed(2);
					this.packagePrice = (this.combo.bl_price * this.num).toFixed(2);
					this.packagePrice = this.packagePrice.toString().split('.')
					if (callback) callback();
				}, 0);
			},
			// 套餐立即购买点击
			async comboBuy() {
				if (this.isDisabled) return;

				//纠正数量
				this.numberChange(true, () => {
					if (!uni.getStorageSync('token')) {
						this.$refs.login.open('/promotionpages/combo/detail/detail?bl_id=' + this.blId);
						return;
					}

					if (this.btnSwitch == false) return;

					this.btnSwitch = false;

					var data = {
						bl_id: this.blId,
						num: this.num
					};

					uni.setStorage({
						key: 'comboOrderCreateData',
						data: data,
						success: () => {
							this.$util.redirectTo('/promotionpages/combo/payment/payment');
							this.btnSwitch = false;
						}
					});
				});
			},
			imageError(index) {
				this.combo.bundling_goods[index].sku_image = this.$util.getDefaultImage().default_goods_img;
				this.$forceUpdate();
			}
		},
		onShareAppMessage(res) {
			var title = '购买套餐，优惠多多哦';
			var path = '/promotionpages/combo/detail/detail?bl_id=' + this.blId;
			return {
				title: title,
				path: path,
				success: res => {},
				fail: res => {}
			};
		}
		
	};
</script>

<style lang="scss">
	.uni-bold {
		font-weight: bold;
	}

	.placeholder {
		height: 55px;
	}

	.combo-package {
		padding-bottom: 160rpx;
		.footer {
			width: 100%;
			background: #fff;
			display: flex;
			justify-content: space-between;
			padding: 20rpx 24rpx;
			box-sizing: border-box;
			position: fixed;
			bottom: 0;
			left: 0;
			
			.footer-info {
				display: flex;
				flex-direction: column;
				justify-content: center;
				.info-price {
					line-height: 1;
					color: #383838;
					display: flex;
					align-items: center;
				}
				.info-num {
					font-size: $ns-font-size-x-sm;
					color: #838383;
					line-height: 1;
					margin-top: 12rpx;
				}
			}
			
			.footer-btn {
				width: 164rpx;
				height: 54rpx;
				border-radius: 27rpx;
				color: #fff;
				font-size: $ns-font-size-sm;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
		.padding-bottom {
			padding-bottom: 80rpx;
		}
	}

	.combo-iphonex {
		padding-bottom: 230rpx;
	}

	.combo-package-content {
		padding-bottom: 40rpx;

		.combo-package-name {
			padding: 16rpx 20rpx;
			background: #ffffff;

			.iconfont {
				display: inline-block;
				font-size: 50rpx;
				vertical-align: middle;
				margin-right: 20rpx;
			}
		}

		.goods-info {
			overflow: hidden;
			margin: 20rpx;
			padding: $ns-padding;
			background: #ffffff;
			border-radius: $ns-border-radius;
			border-bottom: 1px solid rgba(229, 229, 229, 0.5);

			&:last-child {
				margin-bottom: 0;
			}

			.data-info {
				display: inline-block;
				width: 66%;
				float: left;
				position: relative;
				margin-left: 20rpx;

				.goods-name {
					overflow: hidden;
					text-overflow: ellipsis;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					display: -webkit-box;
					line-height: 150%;
					margin-bottom: 20rpx;
				}

				.price-wrap {
					height: 50rpx;
					line-height: 50rpx;

					.num {
						float: right;
					}
				}
			}

			.goods-img {
				display: inline-block;
				width: 30%;
				text-align: center;
				line-height: 100%;
				float: left;

				.img-wrap {
					display: inline-block;
					width: 100%;
					height: 140rpx;
					text-align: center;

					image {
						height: 140rpx;
						width: 100%;
						vertical-align: middle;
					}
				}
			}
		}
	}

	
</style>
